<?php

# 
# Copyright (c) 2002-2007 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration 
# Environment (CABIE)
# 
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying 
# permission or http://www.gnu.org. 
#                                                                            
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR  
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS 
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK. 
#                                                                            
# Permission to modify the code and to distribute modified code is granted, 
# provided the above notices are retained, and a notice that the code was 
# modified is included with the above copyright notice. 
# 

    require_once('auth.php');

    print "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>\n";
    print "<head>\n";
    print "<title>cabie dashboard</title>\n";
    print "<link rel='stylesheet' href='/cabie.css' type='text/css'>\n";

    $val = $_POST{'task'};
    $ref = $_POST{'referer'};

    $sleep = $_REQUEST['sleep'];

    switch ($val) { 
        case 'Cancel':
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=$ref\">";
            break;
        case 'Submit':
            echo "<meta http-equiv=\"REFRESH\" content=\"0;url=$ref\">";
            break;
    }

    $referer = $_SERVER["HTTP_REFERER"];
  
    if ($sleep > 0) {
        echo "<meta http-equiv=\"REFRESH\" content=\"$sleep;url=$referer\">";
    }

?>
</head>
<body>
<script type="text/javascript" src="javascript/wz_tooltip.js"></script>

<?php 

    require_once("dbdefs.php");
    require_once("cmddefs.php");
    # include("common.php");

    require_once('xmlrpc.inc');

    $SQL = new sqlfuncs();
    $CMD = new cmddefs();

    $SQL->__construct($server, $username, $password, $database);

    $port = serverport($SQL, $_REQUEST['server']);

    $_REQUEST['port'] = $port;
    $_REQUEST['who']  = $user;

    $CMD->__construct($_REQUEST);
    
    $cmdarray = $CMD->_returnadmincmd($_REQUEST['cmd']);

    $alt = $cmdarray['alt'];

    $referer = $_SERVER["HTTP_REFERER"];

    $remoteserver = $cmdarray['server'];
    $remoteport   = $cmdarray['port'];
    $command      = $_REQUEST['cmd'];

    $args    = $cmdarray['args'];
    $dotable = $cmdarray['dotable'];

    $user = $_SESSION['PHP_AUTH_USER'];

    $ok = _verifylevel($SQL, $user, $cmdarray['level']);

    if (isset($command)) {

        if ($dotable) {
            $servername = $_REQUEST['server'];
            $sqlarray = _getactiveservers($SQL, 1);
            _displayactiveservers($sqlarray, $servername);
            $port = serverport($SQL, $_REQUEST['server']);
            $isup = _serverup($SQL, $_REQUEST['server']);
            if ($isup) {
                _servermenu($SQL, $_REQUEST['server']);
                if ($ok) {
                    processrequest($_REQUEST, $_REQUEST['cmd'], $CMD, 
                                   $cmdarray, 0);
                } else {
                    errormessage("access to $command denied", 8);
                    echo "</tbody>\n".
                         "</table>\n";
                }
            } else {
                displaymessage($_REQUEST['server'], "server unavailable");
            }
        } else {
    
            if ($ok) {
                if (isset($alt)) {
                   $resp = _server_request("$remoteserver", "$remoteport",
                                       "$alt", "$args");
                } else {
                   $resp = _server_request("$remoteserver", "$remoteport",
                                       "$command", "$args");
                }
                echo "<h2>Processing request, please wait!</h2>\n";
            } else {
                echo "<h2>Access to $command denied</h2>\n";
            }
     
        }

    }
    
?>

</div>

<?php
    $mem = memory_get_usage();
    $usage = sprintf("%d", $mem/1024);
    echo "<p>memory used: ".$usage."k</p>\n";
?>

</body>
</html>

<?php
    exit;
?>
